package com.xgkj.diyiketang.activity;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.MotionEvent;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.TextView;
import android.widget.Toast;

import com.xgkj.diyiketang.http.engine.ShowToastTaskEventListener;
import com.xgkj.diyiketang.task.ZanzhaopianTask;
import com.xgkj.diyiketang.util.PalLog;
import com.xgkj.diyiketang.util.ToastUtil;
import com.xgkj.diyiketang.R;

/**
 * Created by XCKJ_Paladowow on 2016/4/11.
 */
public class JuBaoActivity extends BaseActivity implements View.OnClickListener {
    private Context mContext;
    private Button btn1;
    private Button btn2;
    private Button btn3;
    private Button btn4;
    private Button btn5;
    private Button btn6;
    private Button btn7;
    private TextView back;
    private Button submit;
    private EditText mContent;
    private String mct;
    private int type ;
    private String photourl;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.jubao);
        mContext = this;
        initView();
    }

    private void initView() {
        btn1 = (Button) findViewById(R.id.rvButton1);
        btn1.setOnClickListener(this);
        btn2 = (Button) findViewById(R.id.rvButton2);
        btn2.setOnClickListener(this);
        btn3 = (Button) findViewById(R.id.rvButton3);
        btn3.setOnClickListener(this);
        btn4 = (Button) findViewById(R.id.rvButton4);
        btn4.setOnClickListener(this);
        btn5 = (Button) findViewById(R.id.rvButton5);
        btn5.setOnClickListener(this);
        btn6 = (Button) findViewById(R.id.rvButton6);
        btn6.setOnClickListener(this);
        btn7 = (Button) findViewById(R.id.rvButton7);
        btn7.setOnClickListener(this);
        back = (TextView) findViewById(R.id.back);
        back.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
        mContent = (EditText) findViewById(R.id.content);
        submit = (Button) findViewById(R.id.jubao_submit);
        submit.setOnClickListener(this);
        Intent intent = getIntent();
        type = intent.getIntExtra("type",0);
        photourl = intent.getStringExtra("photourl");
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.rvButton1:
                btn1.setEnabled(false);
                btn2.setEnabled(true);
                btn3.setEnabled(true);
                btn4.setEnabled(true);
                btn5.setEnabled(true);
                btn6.setEnabled(true);
                btn7.setEnabled(true);
                break;
            case R.id.rvButton2:
                btn2.setEnabled(false);
                btn1.setEnabled(true);
                btn3.setEnabled(true);
                btn4.setEnabled(true);
                btn5.setEnabled(true);
                btn6.setEnabled(true);
                btn7.setEnabled(true);
                break;
            case R.id.rvButton3:
                btn3.setEnabled(false);
                btn1.setEnabled(true);
                btn2.setEnabled(true);
                btn4.setEnabled(true);
                btn5.setEnabled(true);
                btn6.setEnabled(true);
                btn7.setEnabled(true);
                break;
            case R.id.rvButton4:
                btn4.setEnabled(false);
                btn1.setEnabled(true);
                btn2.setEnabled(true);
                btn3.setEnabled(true);
                btn5.setEnabled(true);
                btn6.setEnabled(true);
                btn7.setEnabled(true);
                break;
            case R.id.rvButton5:
                btn5.setEnabled(false);
                btn1.setEnabled(true);
                btn2.setEnabled(true);
                btn3.setEnabled(true);
                btn4.setEnabled(true);
                btn6.setEnabled(true);
                btn7.setEnabled(true);
                break;
            case R.id.rvButton6:
                btn6.setEnabled(false);
                btn1.setEnabled(true);
                btn2.setEnabled(true);
                btn3.setEnabled(true);
                btn4.setEnabled(true);
                btn5.setEnabled(true);
                btn7.setEnabled(true);
                break;
            case R.id.rvButton7:
                btn7.setEnabled(false);
                btn1.setEnabled(true);
                btn2.setEnabled(true);
                btn3.setEnabled(true);
                btn4.setEnabled(true);
                btn5.setEnabled(true);
                btn6.setEnabled(true);
                break;
            case R.id.jubao_submit:
                if(checkInput() ){
                    if (type ==1){
                        doZan();
                    }else {
                        ToastUtil.showMessage(mContext,"举报成功");
                        finish();
                    }
                }
                break;
        }
    }

    private void doZan(){
        ZanzhaopianTask task = new ZanzhaopianTask(photourl,"1");
        task.addListener(mListener);
        getEatShowApp().getTaskManager().addTask(task);
    }

    private ShowToastTaskEventListener mListener = new ShowToastTaskEventListener(this){
        @Override
        public void handleTaskCompleted(int taskId, int result, Object data) {
            super.handleTaskCompleted(taskId, result, data);
            ToastUtil.showMessage(mContext,"举报成功");
            finish();
        }

        @Override
        public void handleTaskError(int taskId, int errorCode, String errorMsg) {
            super.handleTaskError(taskId, errorCode, errorMsg);
        }
    };

    private boolean checkInput() {
        mct = mContent.getText().toString().trim();

        if(btn1.isEnabled() && btn2.isEnabled() && btn3.isEnabled() && btn4.isEnabled() && btn5.isEnabled() && btn6.isEnabled() && btn7.isEnabled()){
            Toast.makeText(mContext, R.string.no_jubao_choose, Toast.LENGTH_SHORT)
                    .show();
            return false;
        }else if (TextUtils.isEmpty(mct)) {
            Toast.makeText(mContext, R.string.no_jubao, Toast.LENGTH_SHORT)
                    .show();
            return false;
        }
        return true;
    }
}
